﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HelloWPF.Study.DataBinding.Dispatcher
{
    /// <summary>
    /// TestDispatcher.xaml 的交互逻辑
    /// </summary>
    public partial class TestDispatcher : Window
    {
        public TestDispatcher()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string multiSiteDoubleArrayString = "{Site1:[1.1,1.2],CommonValue:[1.7,1.8]};{Site2:[1.3,1.4]};{CommonValue:[1.5,1.6]}";

            string multiSiteDoubleArrayString2 = multiSiteDoubleArrayString.Replace("[", "\"[").Replace("]", "]\"");


            //Console.WriteLine($"Main ThreadId is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
            MessageBox.Show($"Main thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");

            //App.Current.Dispatcher.Invoke(() =>
            //{
            //    MessageBox.Show($"Dispatcher call thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
            //    this.btnTestDispatcher.Content = "使用Invoke更新";
            //});

            //Task.Run(() =>
            //{
            //    Console.WriteLine($"Sub 1 thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
            //    this.btnTestDispatcher.Content = "Task中更新";
            //});

            //Invoke
            Task.Run(() =>
            {
                MessageBox.Show($"Sub 2 thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
                App.Current.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show($"Sub 2 thread dispatcher call thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
                    this.btnTestDispatcher.Content = "使用Invoke更新";
                });
            });

            //BeginInvoke
            //Task.Run(() =>
            //{
            //    MessageBox.Show($"Sub 3 thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
            //    App.Current.Dispatcher.BeginInvoke((System.Action)(() =>
            //    {
            //        MessageBox.Show($"Sub 3 thread dispatcher call thread id is {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
            //        this.btnTestDispatcher.Content = "使用BeginInvoke更新";
            //    }));
            //});

            Console.Read();
        }
    }
}
